package edu.hut.bookshop.service.impl;

import edu.hut.bookshop.dao.BusinessMapper;
import edu.hut.bookshop.exception.CustomizeException;
import edu.hut.bookshop.pojo.Admin;
import edu.hut.bookshop.pojo.Business;
import edu.hut.bookshop.service.BusinessService;
import edu.hut.bookshop.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessServiceImpl implements BusinessService {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public int insertBusiness(String bName,String bPasswd) {
        Business business = new Business();
        business.setbName(bName);
        business.setbPasswd(bPasswd);
        return businessMapper.insertBusiness(business);
    }

    @Override
    public Business selectBybName(String bName) {
        Business business = new Business();
        business = businessMapper.selectBybName(bName);
        return business;
    }

    @Override
    public Business buslogin(String bName, String bPasswd) {
        //根据用户名称来查询用户的数据是否存在，如果不存在则抛出异常
       Business result =businessMapper.selectBybName(bName);
        if(result==null){
            throw new CustomizeException(ResultCode.USER_NOT_FOUND);
        }

        if(!result.getbPasswd().equals(bPasswd)){
            throw new CustomizeException(ResultCode.PASSWORD_ERROR);
        }

        Business business = new Business();
        business.setbId(result.getbId());
        business.setbPasswd(result.getbPasswd());
        return business;
    }


}
